﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Group3.MediaTool.Data.Generator;
using Group3.MediaTool.Core.ObjectGenerator;
using Group3.MediaTool.Provider;
using Group3.MediaTool.Interface;
using Group3.MediaTool.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using Group3.MediaTool.Data.IO;

namespace Group3.MediaTool.UnitTests
{
    [TestClass]
    public class ProviderTest
    {
        /// <summary>
        /// This test checks, if the wirteoption works for NDatabase with a loaded object.
        /// </summary>
        [TestMethod]
        public void NDatabaseWriteTest()
        {
            //Class init
            E_TestClass2 testclass = new E_TestClass2();
            testclass.Testproperty = "Test";
            //Provider def
            IDataProvider<IObjectRelationModel> provider = new NDatabaseProvider();
            //create database directory
            //if (!Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database")))
            //    Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database"));
            ////set provider-data
            //provider.Connect = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database");
            //provider.Store = "Test.db";
            ////test savechanges
            //Assert.IsTrue(provider.SaveChanges(testclass));
            //Directory.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database"), true);
            //Assert.IsFalse(Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database")));
            if (!Directory.Exists(Path.Combine(StaticConfig.pathDataDefinition, "Database")))
                Directory.CreateDirectory(Path.Combine(StaticConfig.pathDataDefinition, "Database"));
            //set provider-data
            provider.Connect = Path.Combine(StaticConfig.pathDataDefinition, "Database");
            provider.Store = "Test.db";
            //test savechanges
            Assert.IsTrue(provider.SaveChanges(testclass));
            Directory.Delete(Path.Combine(StaticConfig.pathDataDefinition, "Database"), true);
            Assert.IsFalse(Directory.Exists(Path.Combine(StaticConfig.pathDataDefinition, "Database")));
        }

        /// <summary>
        /// This test writes an object to a databasefile and reads a list, including the written object.
        /// </summary>
        [TestMethod]
        public void NDatabaseReadTest()
        {
            //Class init
            E_TestClass2 testclass = new E_TestClass2();
            testclass.Testproperty = "Test";
            //Provider def
            IDataProvider<IObjectRelationModel> provider = new NDatabaseProvider();
            ////create database directory
            //if (!Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database")))
            //    Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database"));
            ////set provider-data
            //provider.Connect = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database");
            //create database directory
            if (!Directory.Exists(Path.Combine(StaticConfig.pathDataDefinition, "Database")))
                Directory.CreateDirectory(Path.Combine(StaticConfig.pathDataDefinition, "Database"));
            //set provider-data
            provider.Connect = Path.Combine(StaticConfig.pathDataDefinition, "Database");
            provider.Store = "Test.db";
            //test savechanges
            Assert.IsTrue(provider.SaveChanges(testclass));
            //reads the data from database
            var testdata = provider.GetDataFromStore<E_TestClass2>();
            Assert.IsNotNull(testdata);
            var testobj = testdata.First();
            Assert.IsTrue(testclass.Testproperty == ((E_TestClass2)testobj).Testproperty);

            //Directory.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database"), true);
            //Assert.IsFalse(Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database")));
            Directory.Delete(Path.Combine(StaticConfig.pathDataDefinition, "Database"), true);
            Assert.IsFalse(Directory.Exists(Path.Combine(StaticConfig.pathDataDefinition, "Database")));
        }

                /// <summary>
        /// This test writes an object to a databasefile and reads a list, including the written object.
        /// </summary>
        [TestMethod]
        public void NDatabaseReadWithPredicateTest()
        {
            //Class init
            E_TestClass2 testclass = new E_TestClass2();
            testclass.Testproperty = "Test";
            //Provider def
            IDataProvider<IObjectRelationModel> provider = new NDatabaseProvider();
            ////create database directory
            //if (!Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database")))
            //    Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database"));
            ////set provider-data
            //provider.Connect = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database");
            //create database directory
            if (!Directory.Exists(Path.Combine(StaticConfig.pathDataDefinition, "Database")))
                Directory.CreateDirectory(Path.Combine(StaticConfig.pathDataDefinition, "Database"));
            //set provider-data
            provider.Connect = Path.Combine(StaticConfig.pathDataDefinition, "Database");
            provider.Store = "Test.db";
            //test savechanges
            Assert.IsTrue(provider.SaveChanges(testclass));
            E_TestClass2 testclass2 = new E_TestClass2();
            testclass2.Testproperty = "Not Test";
            provider.SaveChanges(testclass2);
            //reads the data from database
            var testdata = provider.GetDataFromStore<E_TestClass2>((t) => t.Testproperty == "Test");
            Assert.IsNotNull(testdata);
            var testobj = testdata.First();
            Assert.IsTrue(testclass.Testproperty == ((E_TestClass2)testobj).Testproperty);
            //Directory.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database"), true);
            //Assert.IsFalse(Directory.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Database")));
            Directory.Delete(Path.Combine(StaticConfig.pathDataDefinition, "Database"), true);
            Assert.IsFalse(Directory.Exists(Path.Combine(StaticConfig.pathDataDefinition, "Database")));
        }

       

    }
}
